import axios from "axios";
import { ModalContext } from "../context/ModalContext";
import React, { useContext } from "react";
import wxAuthClient from "../utils/wxAuthClient";
const AxiosInterceptor = () => {
  const { showModal } = useContext(ModalContext);

  axios.interceptors.request.use((config) => {
    // if (!window["icApiSettings"]) {
    //   window["icApiSettings"] = { nonce: "a141377252" };
    // }
    if (window["icApiSettings"]) {
      config.headers["X-WP-Nonce"] = window["icApiSettings"].nonce;
    }
    // console.log('set headers', config.headers, window["icApiSettings"]);
    return config;
  });

  axios.interceptors.response.use(
    (response) => response,
    (error) => {
      if (error.response && error.response.status === 401) {
        wxAuthClient.logout();
        showModal();
      }
      return Promise.reject(error);
    }
  );

  return null;
};

export default AxiosInterceptor;
